<?php

// @formatter:off
/**
 * @file activate.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: activate.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/users/activate.php $
 */
// @formatter:on

require_once __DIR__."/tools.php";

class WCServiceUsersActivate extends WCService {
	//
	// Class methods.
	public function __construct() {
		$this->_readOnlyConflict = true;

		$this->_requiredParams["url"][] = "user";
		$this->_requiredParams["url"][] = "activate";

		$this->_requiredPermissions["to"][] = WC_PERM_USER_REGISTER;
	}
	//
	// Public methods
	public function check() {
		if(parent::check()) {
			global $wcUser;

			if(!$wcUser || !$wcUser->ok()) {
				$this->setError(WC_HTTPERROR_BAD_REQUEST, "No valid user specified");
			}
		}

		return $this->ok();
	}
	//
	// Protected methods.
	protected function load() {
		$out = false;

		if(parent::load()) {
			$this->activate = isset($this->activate) ? trim($this->activate) : false;

			$out = true;
		}

		return $out;
	}
	protected function runGET() {
		global $wcUser;

		if(!$wcUser->hash_register) {
			$this->setError(WC_HTTPERROR_BAD_REQUEST, "User is already activated");
		} elseif($wcUser->hash_register != $this->activate) {
			$this->setError(WC_HTTPERROR_BAD_REQUEST, "Invalid hash code");
		} else {
			$wcUser->hash_register = "";
			$wcUser->hash_dregister = 0;
			$this->_count++;
		}
	}
}
